using System;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;

namespace FireStarterModeller.BO
{
    public class DMRelationshipOnetoOnePropKeyRule : IBusinessObjectRule
    {
        private DMRelationship _dmRelationship;

//        public DMRelationshipOnetoOnePropKeyRule(DMRelationship dmRelationship)
//        {
//            if (dmRelationship == null) throw new ArgumentNullException("dmRelationship");
//            _dmRelationship = dmRelationship;
//        }

        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _dmRelationship = (DMRelationship) bo;
            if (_dmRelationship.ClassDM == null) return true;
            if (_dmRelationship.RelationshipProperties.Count == 0) return true;
            if (_dmRelationship.ReverseRelationship == null) return true;
            if (_dmRelationship.ReverseRelationship.Cardinality != Cardinality.Single) return true;
            return _dmRelationship.HasAllPkProps() || _dmRelationship.HasAllAkProps();
        }

        public string Name
        {
            get { return "DMRelationshipOnetoOnePropKey"; }
        }

        public string Message
        {
            get
            {
                return string.Format("The Properties associated with both sides of the One to One relationship '{0}-{1}' on Classes '{2}-{3}' must be a Primary Key or Alternate Key.", _dmRelationship, _dmRelationship.ReverseRelationship, _dmRelationship.ClassDM, _dmRelationship.RelatedClass);
            }
        }

        public ErrorLevel ErrorLevel
        {
            get { return ErrorLevel.Warning; }
        }
    }
}